package com.hht.toolanduilibs.util

import android.content.Context
import android.graphics.Bitmap
import android.media.MediaScannerConnection
import android.os.Environment
import com.hht.toolanduilibs.bean.ImageResultBean
import com.hht.toolanduilibs.dataFormat
import com.hht.toolanduilibs.showLog
import java.io.File
import java.io.FileOutputStream
import java.io.IOException

/**
 * Author:chen'hu
 * Time:2024/3/19 14:26
 * Description:This is FileUtils
 */
object FileUtils {
    const val FOLDER_CAPTURE = "Capture" //保存图片在本地的路径
    private val IMAGE_PATH =
        Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
            .toString() + File.separator + FOLDER_CAPTURE

    fun saveFile(fileByteData: ByteArray, fileName: String): ImageResultBean {
        var fileName = fileName + System.currentTimeMillis().dataFormat()
        var saveFileSuccess = true
        var file: File? = null
        val filePath = IMAGE_PATH
        var fileOutputStream: FileOutputStream? = null
        fileName = "$fileName.png"
        makeRootDirectory(IMAGE_PATH)
        try {
            file = File(filePath, fileName)
            fileOutputStream = FileOutputStream(file)
            fileOutputStream.write(fileByteData, 0, fileByteData.size)
            fileOutputStream.flush()
            "保存文件成功, 线程为：${Thread.currentThread().name} ,目录为: ${file.absolutePath}".showLog(1)
        } catch (e: Exception) {
            e.printStackTrace()
            saveFileSuccess = false
            "保存文件失败 ${e.message}".showLog(2)
        } finally {
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close()
                } catch (e: IOException) {
                    e.printStackTrace()
                }
            }
        }
        return ImageResultBean(saveFileSuccess, file)
    }

    fun makeRootDirectory(filePath: String) {
        val file: File
        try {
            file = File(filePath)
            if (!file.exists()) {
                file.mkdir()
            }
        } catch (e: java.lang.Exception) {
            e.printStackTrace()
        }
    }

    fun saveFile(bitmap: Bitmap?, fileName: String,mImagePath: String= IMAGE_PATH,context: Context): ImageResultBean {

        var saveFileSuccess = true
        var file: File? = null
        if (bitmap == null) {
            saveFileSuccess = false
        } else {
            var fileName = fileName + System.currentTimeMillis().dataFormat()
            var fileOutputStream: FileOutputStream? = null
            fileName = "$fileName.png"
            makeRootDirectory(mImagePath)
            try {
                file = File(mImagePath, fileName)
                fileOutputStream = FileOutputStream(file)
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream)
                fileOutputStream.flush()
                MediaScannerConnection.scanFile(context,
                    arrayOf(file.path),
                    null,
                    null)

                "保存文件成功, 线程为：${Thread.currentThread().name} ,目录为: ${file.absolutePath}".showLog(1)
            } catch (e: Exception) {
                e.printStackTrace()
                saveFileSuccess = false
                "保存文件失败 ${e.message}".showLog(2)
            } finally {
                if (fileOutputStream != null) {
                    try {
                        fileOutputStream.close()
                    } catch (e: IOException) {
                        e.printStackTrace()
                    }
                }
            }
        }
        return ImageResultBean(saveFileSuccess, file)
    }
}